<?php

/**
 * 自定义资料
 *
 * @package         YDCMS
 * @author          QQ:644157559 <644157559@qq.com>
 * @version         $Id: MylangController.class.php v1.0 2014-06-13 17:27:03 $
 */
namespace Admin\Controller;
class MylangController extends CommonController{
    private $model;
    private $service;
    public function _initialize() {
        parent::_initialize();
        $this->model = D("Mylang");
        $this->service = D("Mylang", "Service");
    }
    
    /**
     * 列表
     */
    public function index() {
        $result = $this->getPagination("Mylang", 1, "*", "id DESC");
        $this->assign("list", $result['data']);
        $this->assign("count", $result['total_rows']);
        $this->assign("page", $result['show']);
        $this->display();
    }
    /**
     * 添加
     */
    public function add() {
        $this->display();
    }
    /**
     * 创建
     */
    public function create() {
        
        $mylang = I("post.mylang");
        if(!isArrayAndNotEmpty($mylang)){
            return $this->errorReturn("无效操作！");
        }
        
        $copyall = I("post.copyall", 0, "intval");
        if($copyall == 1){
            $langs = D("Language", "Service")->getAll(array("status" => 1));
            foreach ($langs as $val) {
                $mylang['lang'] = $val['nid'];
                $result = $this->service->add($mylang);
                if(!$result['status']){
                    return $this->errorReturn($result['data']['error']);
                }
            }
        }else{
            $result = $this->service->add($mylang);
            if(!$result['status']){
                return $this->errorReturn($result['data']['error']);
            }
        }
        return $this->successReturn("添加自定义资料成功", U("index"));
    }
    /**
     * 修改
     */
    public function edit() {
        $id = I("get.id", 0, "intval");
        if($id <= 0){
            return $this->error("无效操作！");
        }
        $mylang = $this->service->getById($id);
        $this->assign("mylang", $mylang);
        //var_dump($mylang);die;
        $this->display();
    }
    /**
     * 更新
     */
    public function update() {
        $mylang = I("post.mylang");
        if(!isArrayAndNotEmpty($mylang)){
            return $this->errorReturn("无效操作！");
        }
        
        $result = $this->service->update($mylang);
        if(!$result['status']){
            return $this->errorReturn($result['data']['error']);
        }

        return $this->successReturn("更新自定义资料成功", U("index"));
    }
    
    /**
     * 删除
     */
    public function delete() {
        $act = I("post.act", "one");
        if($act == "one"){
            $id = I("post.id", 0, "intval");
            if($id <= 0){
                return $this->errorReturn("无效操作！");
            }

            if(false === $this->service->delete($id)){
                return $this->errorReturn("删除失败！");
            }else{
                return $this->successReturn("删除成功！", U("index"));
            }
        }else if($act == "two"){
            $ids = I("post.ids");
            if(!isArrayAndNotEmpty($ids)){
                return $this->errorReturn("无效操作！");
            }
            foreach ($ids as $id) {
                $this->service->delete($id);
            }
            
            return $this->successReturn('删除数据成功！', U('index'));
        }else{
            return $this->errorReturn("无效操作！");
        }
    }
}
